#! /bin/sh
# PCP QA Test No. 187
# for bug #327288 - $PCP_RC_DIR scripts blow away $PCP_TMP_DIR/pmlogger entries
#
# Note ... this will be a "sometimes" failure ... requires special
# combinations of pids such that sort != sort -n and comm gets confused
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard filters
. ./common.product
. ./common.filter
. ./common.check

_cleanup()
{
    _restore_auto_restart pmcd
    $sudo rm -f $tmp.*
}

sts=0
trap "_cleanup; exit \$sts" 0 1 2 3 15
_stop_auto_restart pmcd

signal=$PCP_BINADM_DIR/pmsignal


_burn_pids()
{
    # burn off 1024 pids
    #
    for i in 1 2 3 4 5 6 7 8
    do
	for j in 1 2 3 4 5 6 7 8
	do
	    for k in 1 2 3 4 5 6 7 8
	    do
		for l in 1 2
		do
		    true
		done
	    done
	done
    done
}

_check()
{
    ( echo "this" $this_pid;
      echo "other" $other_pid;
      echo "other-fqdn" $otherfqdn_pid; ) \
    | while read tag pid
    do
	$PCP_ECHO_PROG $PCP_ECHO_N "map file for $tag host""$PCP_ECHO_C"
	if [ -f $PCP_TMP_DIR/pmlogger/$pid ]
	then
	    $PCP_ECHO_PROG " present"
	else
	    $PCP_ECHO_PROG $PCP_ECHO_N " missing ... ""$PCP_ECHO_C"

	    if [ "$tag" = this ]
	    then
		$PCP_ECHO_PROG "expected for this host when PMCD killed"
	    else
		$PCP_ECHO_PROG "NOT EXPECTED"
		echo "pid=$pid, $PCP_TMP_DIR/pmlogger contains"
		ls -l $PCP_TMP_DIR/pmlogger
		echo "pmlogger processes ..."
		ps $PCP_PS_ALL_FLAGS | grep pmlogger
		echo "pmlogger log file for missing one ..."
		cat $tmp.$pid
	    fi
	fi
    done
}

otherhost=`./getpmcdhosts -L -n 1`
[ -z "$otherhost" ] && _notrun "Cannot find a remote host running pmcd"
echo "otherhost=$otherhost" >>$seq_full
otherip=`_host_to_ipaddr $otherhost`
if [ -z "$otherip" ]
then
    echo "Arrgh ... cannot get IP addr for otherhost=$otherhost"
    sts=1
    exit
fi
echo "otherip=$otherip" >>$seq_full

# real QA test starts here
_start_up_pmlogger -L -c /dev/null -l $tmp.log1 -s1 $tmp.this >/dev/null 2>&1
this_pid=$pid
echo "this logger pid=$this_pid" >>$seq_full
_wait_for_pmlogger $this_pid $tmp.log1 || _exit 1
$sudo mv $tmp.log1 $tmp.$this_pid

trap "$sudo $signal \$this_pid \$other_pid \$otherfqdn_pid >/dev/null 2>&1; wait; $sudo rm -fr $tmp.*; _cleanup; exit \$sts" 0 1 2 3 15

_burn_pids

_start_up_pmlogger -h $otherhost -L -c /dev/null -l $tmp.log2 -s1 $tmp.other >/dev/null 2>&1
other_pid=$pid
echo "other host=$otherhost logger pid=$other_pid" >>$seq_full
_wait_for_pmlogger $other_pid $tmp.log2 || _exit 1
$sudo mv $tmp.log2 $tmp.$other_pid

_burn_pids

_start_up_pmlogger -h $otherip -L -c /dev/null -l $tmp.log3 -s1 $tmp.other.fqdn >/dev/null 2>&1
otherfqdn_pid=$pid
echo "other fqdnhost=$otherip logger pid=$otherfqdn_pid" >>$seq_full
_wait_for_pmlogger $otherfqdn_pid $tmp.log3 || _exit 1
$sudo mv $tmp.log3 $tmp.$otherfqdn_pid

# check entries
_check

if ! _service pmcd restart 2>&1; then _exit 1; fi \
| _filter_pcp_restart
_wait_for_pmcd || _exit 1
if ! _service pmlogger restart 2>&1; then _exit 1; fi \
| _filter_pcp_restart
_wait_for_pmlogger || _exit 1

# check again
_check

echo >>$seq_full
echo "=== this log ===" >>$seq_full
cat $tmp.$this_pid >>$seq_full


echo >>$seq_full
echo "=== other log ===" >>$seq_full
cat $tmp.$other_pid >>$seq_full


echo >>$seq_full
echo "=== otherfqdn log ===" >>$seq_full
cat $tmp.$otherfqdn_pid >>$seq_full

